package com.hxht.cmp.service.impl;

import com.hxht.cmp.common.Constants;
import com.hxht.cmp.entity.*;
import com.hxht.cmp.mapper.CaseMapper;
import com.hxht.cmp.mapper.PlanMapper;
import com.hxht.cmp.mapper.TrialMapper;
import com.hxht.cmp.mapper.VideoMapper;
import com.hxht.cmp.pojo.SearchCriteria;
import com.hxht.cmp.service.JudgeService;
import org.apache.commons.collections.map.HashedMap;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class JudgeServiceImpl implements JudgeService {
    @Autowired
    private PlanMapper planMapper;
    @Autowired
    private CaseMapper caseMapper;
    @Autowired
    private VideoMapper videoMapper;
    @Autowired
    private TrialMapper trialMapper;

    @Override
    public List<Map<String, Object>> selectJudgeCasePlanOrReview(SearchCriteria searchCriteria) {
        List<Plan> planList = null;
        if (searchCriteria.getReview().equals("0")) {
            //查询排期
            planList = planMapper.selectJudgeCasePlan(searchCriteria);
        } else {
            //查询回顾
            planList = planMapper.selectJudgeCaseReview(searchCriteria);
        }
        List<Map<String, Object>> mapList = new ArrayList<>();
        if (!planList.isEmpty() && planList != null) {
            List<Case> caseList = caseMapper.selectCaseList(planList);
            for (Plan plan : planList) {
                for (Case aCase : caseList) {
                    if (plan.getCaseUid().equals(aCase.getUid())) {
                        Map<String, Object> map = new HashMap<>();
                        CourtRoom courtRoom = Constants.COURTROOM_MAP.get(plan.getCourtRoomUid());
                        map.put("courtUid", plan.getCourtUid());
                        map.put("name", aCase.getName());
                        map.put("accuse", aCase.getAccuse());
                        map.put("accused", aCase.getAccused());
                        map.put("caseType", plan.getCategory());
                        map.put("courtroomUid", plan.getCourtRoomUid());
                        map.put("courtroomName", courtRoom.getName());
                        map.put("typeName", Constants.CASE_TYPE_MAP.get(plan.getCategory()).getName());
                        map.put("caseUid", aCase.getUid());
                        map.put("planUid", plan.getUid());
                        map.put("status", plan.getStatus());
                        map.put("startDate", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(plan.getStartDate()));
                        if (plan.getStatus() == 0) {
                            map.put("statusName", "排期");
                        } else if (plan.getStatus() == 1 || plan.getStatus() == 5) {
                            map.put("statusName", "正在开庭");
                        } else if (plan.getStatus() == 2) {
                            map.put("statusName", "休庭");
                        } else if (plan.getStatus() == 3) {
                            map.put("statusName", "闭庭");
                        } else if (plan.getStatus() == 4) {
                            map.put("statusName", "归档");
                        }
                        mapList.add(map);
                    }
                }
            }
        }
        return mapList;
    }

    @Override
    public Map<String, Object> selectMoreCasePlanOrReview(SearchCriteria searchCriteria) {
        //设置分页
        searchCriteria.setPageStart((searchCriteria.getPage() - 1) * searchCriteria.getPageSize());
        List<Map<String, Object>> listMap = null;
        int count = 0;
        if (searchCriteria.getReview().equals("0")) {
            //更多开庭
            count = planMapper.getCountMoreCasePlan(searchCriteria);
            listMap = planMapper.selectMoreCasePlan(searchCriteria);
        } else {
            //更多回顾
            count = planMapper.getCountMoreCaseReview(searchCriteria);
            listMap = planMapper.selectMoreCaseReview(searchCriteria);
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<String> planUids = new ArrayList<>();
        for (Map<String, Object> map : listMap) {
            planUids.add(map.get("planUid").toString());
            map.put("courtroomName", Constants.COURTROOM_MAP.get(map.get("courtroomUid")).getName());
            map.put("caseTypeName", Constants.CASE_TYPE_MAP.get(map.get("caseTypeUid")).getName());
            map.put("startDate", format.format(map.get("startDate")));
            map.put("lastUpdated", format.format(map.get("lastUpdated")));
        }

        List<Trial> trials = new ArrayList<>();

        if(planUids.size()>0){
            Example example = new Example(Trial.class);
            example.createCriteria().andIn("planUid",planUids);
            trials = trialMapper.selectByExample(example);
        }

        Map<String, Object> map = new HashMap<>();
        map.put("count", count);
        map.put("data", listMap);
        map.put("trialList",trials);
        return map;
    }

    /**
     * 根据法官ID查询开庭次数
     *
     * @param judgeUids
     * @return
     */
    @Override
    public int getJudgeCourtStatistical(List<String> judgeUids) {
        if (ObjectUtils.isEmpty(judgeUids)) {
            return 0;
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Example example = new Example(Plan.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("judgeUserUid", judgeUids);
        criteria.andGreaterThan("status", 1);
        String today = format.format(new Date());
        String year = today.substring(0, 4) + "-01-01";
        criteria.andLessThanOrEqualTo("startDate", today);
        criteria.andGreaterThan("startDate", year);
        criteria.andEqualTo("active",1);
        List<Plan> plans = planMapper.selectByExample(example);
        return plans.size();
    }

    /**
     * 根据法官ID查询开庭时长
     *
     * @param judgeUids
     * @return
     */
    @Override
    public int getJudgeCourtTimeStatistical(List<String> judgeUids) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String today = format.format(new Date());
        String year = today.substring(0, 4) + "-01-01";
        return planMapper.getJudgeCourtTimeStatistical(judgeUids, year, today);
    }

    /**
     * 查询开庭视频总容量
     *
     * @param judgeUids
     * @param courtroomUid
     * @return
     */
    @Override
    public int getJudgeFileSizeStatistical(List<String> judgeUids, String courtroomUid) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String today = format.format(new Date());
        String year = today.substring(0, 4) + "-01-01";
        return videoMapper.getJudgeFileSizeStatistical(judgeUids, courtroomUid, year, today);
    }

    @Override
    public List<Map<String, Object>> getJudgeOpeningCourtTimeRanking(String courtUid) {
        return trialMapper.getJudgeOpeningCourtTimeRanking(courtUid);
    }

    @Override
    public List<Map<String, Object>> getJudgeOpeningCourtNumRanking(String courtUid) {
        return planMapper.getJudgeOpeningCourtNumRanking(courtUid);
    }

    @Override
    public Map<String, Object> getJudgeCourtMonthStatistics(List<String> judgeUids, String courtUid, String startDate, String endDate) {
        Map<String, Object> result = new HashedMap();
        result.put("timeMonthStatistics", planMapper.getJudgeCourtTimeMonthStatistical(judgeUids, courtUid, startDate, endDate));
        result.put("countMonthStatistics", planMapper.getJudgeCourtCountMonthStatistical(judgeUids, courtUid, startDate, endDate));
        return result;
    }
}